//
//  LoginViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "SetBlack.h"
#import "BlackManager.h"
@interface LoginViewController ()
@property (nonatomic, retain)UITextField *nameTextField;
@property (nonatomic, retain)UITextField *pwdTextField;
@property (nonatomic, retain)UILabel *name;
@property (nonatomic, retain)UILabel *pwd;
@end

@implementation LoginViewController
-(void)dealloc
{
    [_name release];
    [_pwd release];
    [_pwdTextField release];
    [_nameTextField release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //账号
    self.name = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 10, 90, self.view.frame.size.width / 8, self.view.frame.size.width / 8)];
    _name.text = @"账号:";
    [self.view addSubview:_name];
    [_name release];
    self.navigationItem.title = @"登录";
    self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(_name.frame.origin.x + _name.frame.size.width, _name.frame.origin.y, self.view.frame.size.width - self.view.frame.size.width / 3, _name.frame.size.height)];
//    nameTextField.backgroundColor = [UIColor redColor];
    _nameTextField.placeholder = @"请输入账号";
    _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameTextField];
    [_nameTextField release];
    
    //密码
    self.pwd = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 10, 100 + _name.frame.size.height, self.view.frame.size.width / 8, self.view.frame.size.width / 8)];
    _pwd.text = @"密码:";
    [self.view addSubview:_pwd];
    [_pwd release];
    
    //lable的夜间模式
    if ([BlackManager sharedInstance].isNight) {
        _name.textColor = [UIColor whiteColor];
        _pwd.textColor = [UIColor whiteColor];
    }else{
         _name.textColor = [UIColor blackColor];
        _pwd.textColor = [UIColor blackColor];
    }

    
    self.pwdTextField = [[UITextField alloc]initWithFrame:CGRectMake(_pwd.frame.origin.x + _pwd.frame.size.width, _pwd.frame.origin.y, self.view.frame.size.width - self.view.frame.size.width / 3, _name.frame.size.height)];
    _pwdTextField.placeholder = @"请输入密码";
    _pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_pwdTextField];
    [_pwdTextField release];
    
    //找回密码
    UIButton *findPwd = [UIButton buttonWithType:UIButtonTypeSystem];
    findPwd.frame = CGRectMake(self.view.frame.size.width / 4 * 3, _pwdTextField.frame.origin.y + _pwdTextField.frame.size.height + 10, self.view.frame.size.width / 4, 30);
    [findPwd setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [findPwd setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:findPwd];
    
    
    //登陆按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    loginBtn.frame = CGRectMake(_name.frame.origin.x, self.view.frame.size.height / 2 - 20, self.view.frame.size.width / 4, 30);
    loginBtn.backgroundColor = [UIColor cyanColor];
    [loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:loginBtn];
    
    //注册按钮
    UIButton *RegisterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    RegisterBtn.frame = CGRectMake(loginBtn.frame.origin.x + loginBtn.frame.size.width + 100, self.view.frame.size.height / 2 - 20, self.view.frame.size.width / 4, 30);
    RegisterBtn.backgroundColor = [UIColor orangeColor];
    [RegisterBtn setTitle:@"注册" forState:UIControlStateNormal];
    [RegisterBtn addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:RegisterBtn];
}
#pragma mark - 点击屏幕收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
#pragma mark - 夜间模式设置
//设置夜间模式  重写BaseViewController中的方法
-(void)setNightDayModel
{
    [SetBlack setBackgroundColorWithView:self.view];
//    [SetBlack setLableTitleColorWithArr:@[_name]];
    //改变导航栏和tabbar的背景颜色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.tabBarController.tabBar.barStyle = UIBarStyleBlack;
}
-(void)setLightDayModel
{
    [SetBlack setBackgroundColorWithView:self.view];
   
    //改变导航栏和tabbar的背景颜色
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.tabBarController.tabBar.barStyle = UIBarStyleDefault;
    
}

#pragma mark - 登录方法
-(void)loginAction
{
    //从本地路径中解档数据
    NSString *namePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"name.a"];
    NSMutableArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:namePath];
    NSString *pwdPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"pwd.a"];
    NSMutableArray *pwdArr = [NSKeyedUnarchiver unarchiveObjectWithFile:pwdPath];
    //记录数据在数组中的位置,通过姓名获取到的位置即密码和照片在各自数组中的位置
    NSInteger i = 0;
    //判断账户是否存在
    if ([nameArr containsObject:_nameTextField.text]) {
        for (i; i < nameArr.count; i++) {
            if ([_nameTextField.text isEqualToString:nameArr[i]]) {
                NSLog(@"%ld", i);
                break;
            }
        }
        //把用户信息在数组中的位置放在单例里存储
        NSNumber *index = [NSNumber numberWithInteger:i];
        [[NSUserDefaults standardUserDefaults]setObject:index forKey:@"index"];
        //判断密码是否与账号对应
        if ([pwdArr[i] isEqualToString:_pwdTextField.text]) {
            //如果账号已经存在
            //设置名字为单例
            [[NSUserDefaults standardUserDefaults]setObject:_nameTextField.text forKey:@"name"];
            //设置账号的图片为现在单例内容
            NSString *picturePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"picture.a"];
            NSMutableArray *pictureArr = [NSKeyedUnarchiver unarchiveObjectWithFile:picturePath];
            NSNumber *i = [[NSUserDefaults standardUserDefaults]objectForKey:@"index"];
            NSInteger index = i.intValue;
            NSString *str = [pictureArr objectAtIndex:index];
            NSData *data = [pictureArr objectAtIndex:index];
            [[NSUserDefaults standardUserDefaults]setObject:data forKey:@"img"];

            //设置登陆状态
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"register"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
             NSLog(@"用户名或密码错误");
            //弹窗
            UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户密码不匹配" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:promptAlert animated:YES completion:^{
                
            }];
            [self dismissViewControllerAnimated:YES completion:^{
                //
            }];

        }
    }else{
        //弹窗
        UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户不存在" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:promptAlert animated:YES completion:^{
            
        }];
        [self dismissViewControllerAnimated:YES completion:^{
            //
        }];

    }
    [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"OtherLogin"];
}
-(void)registerAction
{
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
    [registerVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
